//
//  MyCollectView.swift
//  maosheng
//
//  Created by lxl on 2019/8/12.
//

import UIKit
enum PageType {
    case collect//收藏
    case like//点赞
    case browse//浏览

}
class MyCollectView: PublicSwiftTableView {
    let cellIdentifer = "MyCollectCell"
    let noImgIdentifer = "MyCollectNoImgCell"
    var type : PageType = .collect
//    var isCollect : Bool = true
    var str_url = "api/services/app/Blog/GetMyFavoriteBlogPagingListAsync"

    override init(frame: CGRect) {
        super.init(frame: frame)
        tableView.register(MyCollectCell.self, forCellReuseIdentifier: cellIdentifer)
        tableView.register(MyCollectNoImgCell.self, forCellReuseIdentifier: noImgIdentifer)

        getTheCollectList(isHeader: true)
        refreshList = {[weak self] (isHeader) in
            self?.getTheCollectList(isHeader: isHeader)
            
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func getTheCollectList(isHeader: Bool){
        let url =  str_url + "?PageIndex=\(pageIndex)&PageSize=10"

        SDataCenter.shared.SGetJsonDataWithUrl(str_url: url, params: nil) { [weak self] (responseData, isSuccess) in
            if self?.tableView.mj_header != nil{
                self?.tableView.mj_header.endRefreshing()
                self?.tableView.mj_footer.endRefreshing()
            }
            
            if isSuccess && responseData is [String : Any]{
                if isHeader{
                    self?.dataSource.removeAllObjects()
                }
                if let items = (responseData as! [String : Any])["items"]{
                    if let arr = items as? Array<Any>{
                        if let attentionArr = [CollectModel].deserialize(from: arr){
                            self?.dataSource.addObjects(from: attentionArr as [Any])
                        }
                    }
                    
                }
                self?.checkNeedNoDataView()
                self?.setRefreshFooter()
            }else{
                if !isHeader{
                    self?.pageIndex = self!.pageIndex - 1
                }
                
            }
        }
    }
    func checkNeedNoDataView(){
        if self.dataSource.count == 0 {
            var str_msg : String?
            switch self.type {
            case .collect:
                str_msg = "还没有任何收藏哦"
            case .like:
                str_msg = "还没有任何点赞哦"
            case .browse:
                str_msg = "还没有任何浏览哦"
         
            }
            self.showTheNoDataView(title: str_msg!)
        }else{
            if self.noDataView != nil{
                self.noDataView?.isHidden = true
            }
        }
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  
        
        let model = self.dataSource[indexPath.row] as? CollectModel
        let images = model?.images
        var cell : MyCollectNoImgCell?
        if images != nil && images!.count > 0{
             cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifer) as! MyCollectCell
        }else{
            cell = tableView.dequeueReusableCell(withIdentifier: noImgIdentifer) as? MyCollectNoImgCell
        }
        cell?.deleteBtn.addTarget(self, action: #selector(deleteCollectOrLike(btn:)), for: .touchUpInside)
        
        switch self.type {
        case .like:
            cell?.deleteBtn.setTitle("删除点赞", for: .normal)
        case .browse:
            cell?.deleteBtn.setTitle("删除浏览记录", for: .normal)
        default:
            print("")
        }
//        if !isCollect{
//            cell?.deleteBtn.setTitle("删除点赞", for: .normal)
//        }
        cell?.deleteBtn.tag = 401 + indexPath.row
        cell!.setCollectInfo(model: model!)
        cell?.selectionStyle = .none
        return cell!
    }
    //MARK:删除点赞或收藏
    @objc func deleteCollectOrLike(btn:UIButton){
        
        PublicTools.showAlert(currentVC: nil, meg: "确定删除", cancelBtn: "取消", otherBtn: "确定") {[weak self] (action) in
            self?.deleteAction(btn: btn)
        }
        
        
    
    }
    
    func deleteAction(btn:UIButton) -> Void {
        let btnTag = btn.tag - 401
        
        var delUrl : String?
        switch self.type {
        case .collect:
            delUrl = "api/services/app/Favorite/DeleteFavoriteAsync"
        case .like:
            delUrl = "api/services/app/Attitude/DeleteAttitudeRecordAsync"
        case .browse:
            delUrl = "api/services/app/ViewRecord/DeleteViewRecordAsync"
        }
        
        
        let model = self.dataSource[btnTag] as? CollectModel
        var params = [String : Any]()
        params["moduleTypeId"] = 100301
        params["objectId"] = model?.objectId
//        if isCollect{
//            
//        }
        
        SDataCenter.shared.SDeleteDataWithUrl(str_url: delUrl!, params: params) { [weak self] (responseData, isSuccess) in
            if isSuccess{
                //                self?.getTheCollectList(isHeader: true)
                self?.dataSource.removeObject(at: btnTag)
                self?.tableView.reloadData()
                self?.checkNeedNoDataView()

            }
        }
    }
}
